<?php

    function create_code(){
        return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }
	//这里是公用函数
	function p($arr){
	echo '<pre>'.print_r($arr,true).'</pre>';

	}
	
	function attach($attach, $type) {
		if (false === strpos($attach, 'http://')) {
			//本地附件
			return __ROOT__ . '/' . C('pin_attach_path') . $type . '/' . $attach;
			//远程附件
			//todo...
		} else {
			//URL链接
			return $attach;
		}
	}
    
	function update_config($new_config, $config_file = '',$siteId=0) {
        !is_file($config_file) && $config_file = CONF_PATH . 'Home/config.php';
        if (is_writable($config_file)) {
            $config = require $config_file;
            if($siteId>0){
                unset($config[$siteId]);
                $config[$siteId] = $new_config;
            }else{
                $config = array_merge($config, $new_config);
            }
            file_put_contents($config_file, "<?php \nreturn " . stripslashes(var_export($config, true)) . ";", LOCK_EX);
            @unlink(RUNTIME_FILE);
            return true;
        } else {
            return false;
        }
    }
	
	function Y($val,$condition){
		if($_GET){
			$val=$_GET["$val"];
		}
		if($_POST){
			$val=$_POST["$val"];
		}
		$data=$condition($val);
		return $data;
	}

	//递归重组节点多维数组
	function nodeMerge($node,$pid=0){
		$arr=array();
		foreach($node as $v){
			if($v['pid']==$pid){
				$v['child']=nodeMerge($node,$v['id']);
				$arr[]=$v;
			}
		}
		return $arr;
	}

	//递归权限中的多维数组
	function accessMerge($node,$access=null,$pid=0){
		$arr=array();
		foreach($node as $v){
			if(is_array($access)){
				$v['access']=in_array($v['id'],$access)? 1 : 0;
			}
		
			if($v['pid']==$pid){
				$v['child']=accessMerge($node,$access,$v['id']);
				$arr[]=$v;
			}
		}
		return $arr;
	}


function getArticleName($id=0,$all=false,$field='title,site_id',$model='article'){
    if($id){
        $info = D($model)->where(array('id'=>$id))->field($field)->find();
        return $all ? $info : $info['title'];
    }else{
        return '';
    }

}
function getCateName($cateid,$siteid){
    $obj = D("ArticleCate");
    $info = $obj->where(array('site_id'=>$siteid,'id'=>$cateid))->getField('name');
    return $info;
}
function getModel($key=''){
    $arr = array(
        'article' => '文章',
        'product' => '产品'
    );
    if($key){
        return array_key_exists($key,$arr) ? $arr[$key] : '';
    }else{
        return $arr;
    }
}
function setModel($key=''){
    $arr = array(
        'article' => '文章',
        'product' => '产品'
    );
    if($key){
        return array_key_exists($key,$arr) ? $arr[$key] : '';
    }else{
        return $arr;
    }
}

function string2array($data) {
    if($data == '') return array();
    $data = stripslashes($data);
    @eval("\$array = $data;");
    return $array;
}
/**
 * 将数组转换为字符串
 *
 * @param	array	$data		数组
 * @param	bool	$isformdata	如果为0，则不使用new_stripslashes处理，可选参数，默认为1
 * @return	string	返回字符串，如果，data为空，则返回空
 */
function array2string($data, $isformdata = 1) {
    if($data == '') return '';
    if($isformdata) $data = new_stripslashes($data);
    return addslashes(var_export($data, TRUE));
}

function new_addslashes($string){
    if(!is_array($string)) return addslashes($string);
    foreach($string as $key => $val) $string[$key] = new_addslashes($val);
    return $string;
}

/**
 * 返回经stripslashes处理过的字符串或数组
 * @param $string 需要处理的字符串或数组
 * @return mixed
 */
function new_stripslashes($string) {
    if(!is_array($string)) return stripslashes($string);
    foreach($string as $key => $val) $string[$key] = new_stripslashes($val);
    return $string;
}

/**
 * 返回经htmlspecialchars处理过的字符串或数组
 * @param $obj 需要处理的字符串或数组
 * @return mixed
 */
function new_html_special_chars($string) {
    $encoding = 'utf-8';
    if(strtolower(CHARSET)=='gbk') $encoding = 'ISO-8859-15';
    if(!is_array($string)) return htmlspecialchars($string,ENT_QUOTES,$encoding);
    foreach($string as $key => $val) $string[$key] = new_html_special_chars($val);
    return $string;
}
?>